# -*- coding: utf-8 -*-

from django import forms
from models import Event, AssetType, Asset
from django.contrib.auth.models import User
from datetime import datetime

def asset_selector():
    types_qs =  AssetType.objects.order_by('name')
    asset_types = []
    
    for i in types_qs:
        asset_types.append((i.id, i.name))
    
    return tuple(asset_types)
    
class EventForm(forms.Form):
    title = forms.CharField(max_length=100, label="Titulo", widget=forms.TextInput)
    description = forms.CharField(required=False, label="Descricao", widget=forms.Textarea) 
    date = forms.DateField(required=True, label="Data", input_formats=['%d/%m/%Y'], widget=forms.DateInput)
    status = False 
    
    def save(self, user_id):
        cd = self.cleaned_data
                        
        event = Event(title = cd["title"],
                description = cd["description"],
                creation_date = datetime.now(),
                date = cd["date"],
                approved = 0
        )
        event.creator = User.objects.get(pk = user_id)
        event.save()

        return event
    
class AssetForm(forms.Form):
    types = forms.ChoiceField(asset_selector(), label="Tipo")
    link = forms.CharField(max_length=100, label="URL", widget=forms.TextInput)
    caption = forms.CharField(max_length=100, label="Legenda", required=False)
    credit = forms.CharField(max_length=100, label="Créditos", required=False)
    
    def save(self, user_id):
        cd = self.cleaned_data
        
        asset = Asset(media = cd["link"],
                      caption = cd["caption"], 
                      creation_date = datetime.now(),
                      approved=0)
        asset.asset_type = AssetType.objects.get(pk = cd["types"])
        asset.creator = User.objects.get(pk = user_id)
        asset.save()
        
        return asset